import { Component, OnInit } from '@angular/core';
import {EvaluateHistoryService} from './evaluate-history.service';
import {NzMessageService} from 'ng-zorro-antd';
import {PageEntity} from '../../../core/domain/page.entity';
import {StatusEnum} from '../../../core/enum/status.enum';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum} from '../../../core/enum/filter.enum';
import {HTTP_BASE_URL, SERVERS_NAME} from '../../../config/config';

@Component({
  selector: 'app-evaluate-history',
  templateUrl: './evaluate-history.component.html',
  styleUrls: ['./evaluate-history.component.scss'],
  providers: [EvaluateHistoryService]
})
export class EvaluateHistoryComponent implements OnInit {
  pageInfo: PageEntity = new PageEntity();
  dataList = [];
  nzWidthConfigs = [];
  searchParams: SearchParams = new SearchParams();
  yearList = [];
  semesterList = [];
  view = false;
  evaluateTableObj: EvaluateTableObj = new EvaluateTableObj();
  evaluateObj: any = {};
  assessmentPersonArr = [
    {label: '学院领导', value: '学院领导', checked: false},
    {label: '督导员', value: '督导员', checked: false},
    {label: '系部负责人', value: '系部负责人', checked: false},
    {label: '职能部门负责人', value: '职能部门负责人', checked: false},
  ];
  checkerRole = [];
  evaluateProjectList = [];
  evaluateProjectNoScoreList = [];
  rowSpanIndexArr = [];
  tdHiddenIndexArr = [];
  totalScore = 0;
  fileList = [];
  linkInfo = `${HTTP_BASE_URL}${SERVERS_NAME.SECURITY_SERVER}`;
  constructor(
    private http: EvaluateHistoryService,
    private message: NzMessageService
  ) { }

  ngOnInit() {
    this.getSchoolYearList();
  }

  /**
   * 获取学年学期
   */
  getSchoolYearList() {
    this.http.getSchoolYearApi().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.yearList = res.datas;
        res.datas.map(item => {
          if (item.currSchoolYear) {
            // this.searchParams.schoolYear = item.schoolYear;
            this.semesterList = item.semester;
            item.semester.map(ele => {
              if (ele.currSemester) {
                // this.searchParams.semester = ele.semester;
              }
            });
          }
        });
        this.clickSearch();
      }
    });
  }

  page(page) {
    this.pageInfo.multiSortMeta = [
      {
        field: 'gmtCreate',
        order: -1
      }
    ];
    this.http.getEvaluateHistoryApi(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.dataList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
        this.nzWidthConfigs = [
          '60px', '150px', '100px', '150px', '150px',
          '100px', '100px', '180px', '100px', '100px',
          '100px', '150px', '200px',
          '200px', '100px', '100px'
        ];
      }
    });
  }

  /**
   * 点击查询
   */
  clickSearch() {
    for (const o in this.searchParams) {
      if (this.searchParams[o]) {
        this.pageInfo.filters[o] = PaginationUtils.filters(this.searchParams[o], FilterEnum.CONTAINS);
      } else {
        delete this.pageInfo.filters[o];
      }
      if (this.searchParams.semester === 0) {
        this.pageInfo.filters.semester = PaginationUtils.filters(this.searchParams.semester, FilterEnum.EQUALS);
      }
    }
    this.pageInfo.first = 1;
    this.page(this.pageInfo);
  }

  /**
   * 点击重置
   */
  resetSearch() {
    this.searchParams = new SearchParams();
    this.pageInfo = new PageEntity();
    this.clickSearch();
  }

  changePageIndex(index) {
    this.pageInfo.first = index;
    this.page(this.pageInfo);
  }

  changePageSize(size) {
    this.pageInfo.first = 1;
    this.pageInfo.rows = size;
    this.clickSearch();
  }

  /**
   * 点击详情
   */
  clickEvaluatedInfo(data) {
    this.http.getEvaluateInfoApi({
      id: data.id
    }).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.view = true;
        this.evaluateObj = res.datas;
        this.evaluateTableObj = res.datas;
        this.evaluateProjectList = [];
        this.evaluateProjectNoScoreList = [];
        this.rowSpanIndexArr = [];
        this.tdHiddenIndexArr = [];
        this.checkerRole = res.datas.checkerRole.split(',');
        this.assessmentPersonArr.map(item => {
          item.checked = this.checkerRole.includes(item.value);
        });
        this.totalScore = 0;
        (res.datas.classifyList || []).map(item => {
          this.totalScore += item.getScore;
          if (item.itemList) {
            // 有分数，有选项
            (item.itemList || []).map(ele => {
              if (ele.type === '文本框') {
                this.evaluateProjectList.push({
                  hasItem: true,
                  rowInfo: {
                    ...ele,
                    examItemName: ele.examItemName
                  },
                  ...item
                });
              } else {
                this.evaluateProjectList.push({
                  hasItem: true,
                  rowInfo: {
                    ...ele,
                    examItemName: ele.examItemName + '(' + ele.selectStr + ')'
                  },
                  ...item
                });
              }
            });
          } else if (item.examClassifyScore) {
            this.evaluateProjectList.push({
              // 有分数，无选项
              hasItem: false,
              rowInfo: {...item},
              ...item
            });
          } else {
            // 无分数
            this.evaluateProjectNoScoreList.push({
              rowInfo: item,
              ...item
            });
          }
        });
        let totalNum = 0;
        this.evaluateProjectList.map((item, index) => {
          if (item.itemList) {
            if (totalNum === index) {
              this.rowSpanIndexArr.push(item.itemList.length);
              totalNum += item.itemList.length;
              this.tdHiddenIndexArr.push(totalNum);
            } else {
              this.rowSpanIndexArr.push(0);
            }
          } else {
            this.tdHiddenIndexArr.push(totalNum += 1);
          }
        });
        this.tdHiddenIndexArr.unshift(0);
        console.log(this.tdHiddenIndexArr);
        console.log(this.evaluateProjectList);
        this.fileList = res.datas.sourceList || [];
      }
    });
  }

  /**
   * 点击附件
   */
  downloadFile(data) {
    window.open(this.linkInfo + 'source/view/' + data.moduleName + '/' + data.fileNewName, '_blank');
  }

}
class SearchParams {
  schoolYear: string;
  semester: any;
  number: string;
  name: string;
  teacher: string;
}

class EvaluateTableObj {
  evaluteDate: string;
  startSection: number;
  endSection: number;
  shouldCount = 0;
  realCount = 0;
  lateCount = 0;
  missCount = 0;
  content: string;
  courseId: string;
  courseName: string;
  attendance: any;
}
